package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarVehicleLoadType;
import com.navinfo.platform.trip.common.arithmetic.enums.LoadTypeEnum;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.vehicle.VehicleInfo;

import java.util.Optional;

/**
 * 东风新增载荷算法<br/>
 * 依赖{@link VehicleInfo}的发动机输出最大扭距、满半空阀值<br/>
 * 输出：统计(amt载重、vecu载重、载重类型)<br/>
 * @author web
 */
public class VehicleLoadType_DF extends AbstractArithmetic<VarVehicleLoadType> {
    /**
     * 重量状态变化阀值，单位kg
     */
    private final int loadThreshold = 3000;

    @Override
    public void calculate() {
        logger.debug("===============东风载荷算法开始===========");
        CommonData commonData = getCommonData();
        Integer amtLoadNew = Optional.ofNullable(commonData.getAmtLoad()).orElse(0);
        Integer vecuLoadNew = Optional.ofNullable(commonData.getVecuLoad()).orElse(0);

        //无效值不参与计算
        if (amtLoadNew==0 && vecuLoadNew==0){
            return;
        }

        VarVehicleLoadType var = getObj();
        // 与上个值相比的变化范围是否超过阈值
        if (var.getAmtLoad()==null || Math.abs(amtLoadNew - var.getAmtLoad()) >= loadThreshold){
            var.setAmtLoad(amtLoadNew);
        }
        if (var.getVecuLoad()==null || Math.abs(vecuLoadNew - var.getVecuLoad()) >= loadThreshold){
            var.setVecuLoad(vecuLoadNew);
        }

        logger.debug("===============东风载荷算法结束===========");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarVehicleLoadType var = getObj();
        statisticDataOut.setAmtLoad_DF(var.getAmtLoad());
        statisticDataOut.setVecuLoad_DF(var.getVecuLoad());

        /**
         * 载荷状态计算
         *      如果车型不在列表内则载荷状态显示为-1
         *      如果车辆VECU和AMT均上报载重以VECU为准
         *   车型载重状态分四段计算
         *   例：
         *   车 || 空载   ||  半载     ||    满载    ||  超载
         *   a  ||＜22.5  || 22.5-50   ||   50-60    ||  ＞60
         */
        LoadTypeEnum loadTypeEnum = LoadTypeEnum.UNDEF;
        VehicleInfo vehicleInfo = drivingData.getVehicleInfo();
        if (vehicleInfo != null) {
            Integer empty = vehicleInfo.getWloadEmpty();
            Integer half = vehicleInfo.getWloadHalf();
            Integer full = vehicleInfo.getWloadFull();
            logger.debug("===============东风载荷算法车辆{}，对应的满半空阀值{}，{}，{}===========", terminalId, empty, half, full);
            if(empty!=null && half!=null && full!=null){
                //最终计算使用的在种植
                int loadValue;
                if (var.getVecuLoad()!=null && var.getVecuLoad()!=0){
                    loadValue = var.getVecuLoad();
                }else {
                    loadValue = Optional.ofNullable(var.getAmtLoad()).orElse(0);
                }

                if(loadValue>0) {
                    if (loadValue <= empty*100) {
                        loadTypeEnum = LoadTypeEnum.EMPTY;
                    } else if (loadValue <= half*100) {
                        loadTypeEnum = LoadTypeEnum.HALF;
                    } else if (loadValue <= full*100) {
                        loadTypeEnum = LoadTypeEnum.FULL;
                    } else {
                        loadTypeEnum = LoadTypeEnum.OVER;
                    }
                }
            }
        }

        statisticDataOut.setLoadType_DF(loadTypeEnum.getValue());
    }

}
